<?php

namespace Models;

/**
 * Description of User
 *
 * @author Admin
 */
class Word extends \Core\Base\Table
{
	
	public function getList()
	{
		$rowset = $this->_db->select('SELECT * FROM word');
		
		return $rowset;
	}
	
	public function getRandomCart($countWord = 10, $userId = null)
	{
		$rowset = $this->_db->select('SELECT * FROM word {WHERE user_id = ?d} ORDER BY RAND() LIMIT ?d', ($userId ? $userId : DBSIMPLE_SKIP), $countWord);
		
		return $rowset;
	}
	
	public function getByWord($word)
	{
		if(!$word)
			return array();
		
		$row = $this->_db->selectRow('SELECT * FROM ?_word WHERE word = ?', trim($word));
		
		return $row;
	}
	
	public function getRandomWord()
	{
		$row = $this->_db->selectRow('SELECT * FROM word ORDER BY RAND() LIMIT 1');
		
		return $row;
	}
	
	public function addWord($data)
	{
		if(empty($data['word']) || empty($data['translate']))
			return false;
		
		$row  = array(
			'user_id' => $data['user_id'],
			'vocabulary_id' => $data['vocabulary_id'],
			'word' => $data['word'],
			'translate' => $data['translate'],
			'lang' => $data['lang'],
		);

		$itemId = $this->_db->selectRow('INSERT INTO word (?#) VALUES (?a)', array_keys($row), array_values($row));
		
		return $itemId;
	}
	
}
